package com.zxl.gtion.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxl.gtion.dto.ArticleDTO;
import com.zxl.gtion.dto.PageDTO;
import com.zxl.gtion.entity.ArticleInfo;
import com.zxl.gtion.entity.Catalog;
import com.zxl.gtion.entity.CatalogArticle;
import com.zxl.gtion.enums.CatalogEnums;
import com.zxl.gtion.service.ArticleInfoService;
import com.zxl.gtion.service.CatalogArticleService;
import com.zxl.gtion.service.CatalogService;
import com.zxl.gtion.util.R;
import com.zxl.gtion.vo.ArticleInfoVO;
import com.zxl.gtion.vo.VaccineVO;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * (Catalog)表控制层
 *
 * @author 
 * @since 2024-03-06 16:43:15
 */
@RestController
@RequestMapping("catalog")
@RequiredArgsConstructor
public class CatalogController  {
    /**
     * 服务对象
     */
    private final CatalogService catalogService;
    private final CatalogArticleService catalogArticleService;
    private final ArticleInfoService articleInfoService;

    /**
     * 首页查询数据
     *
     * @param catalog 查询实体
     * @return 所有数据
     */
    @GetMapping("homePage")
    @ApiOperation("疫苗知识/新闻/政策 疫苗知识type传0 新闻type传1 政策type传2")
    public R<List<ArticleInfo>> homePage(Catalog catalog) {
        return R.ok(this.catalogService.homePage(catalog));
    }

    @GetMapping("searchVaccine")
    @ApiOperation("搜索框展示疫苗信息")
    public R<List<Catalog>> searchVaccine(Catalog catalog) {
        return R.ok(catalogService.lambdaQuery()
                .like(StringUtils.isNotEmpty(catalog.getName()), Catalog::getName, catalog.getName())
                .notIn(Catalog::getType, CatalogEnums.getNotVaccines())
                .list());
    }

    @GetMapping("getContentByCatalogId")
    @ApiOperation("根据疫苗id获取文章信息")
    public R<Catalog> getContentByCatalogId(@RequestParam String id) {
        List<CatalogArticle> list = catalogArticleService.lambdaQuery().eq(CatalogArticle::getCatalogId, id).orderByDesc(CatalogArticle::getCreateTime).list();
        if (list.isEmpty()) {
            return R.ok();
        }
        CatalogArticle catalogArticle = list.get(0);
        Integer articleId = catalogArticle.getArticleId();
        Catalog byId = catalogService.getById(id);
        ArticleInfoVO articleInfoVO = articleInfoService.selectInfoById(articleId);
        byId.setContent(articleInfoVO.getContent());
        return R.ok(byId);
    }

    @GetMapping("homeVaccine")
    @ApiOperation("首页疫苗知识")
    public R<List<Catalog>> homeVaccine() {
        return R.ok(this.catalogService.homeVaccine());
    }

    @GetMapping("moreVaccine")
    @ApiOperation("更多疫苗知识")
    public R<List<VaccineVO>> moreVaccine() {
        return R.ok(this.catalogService.moreVaccine());
    }

    @GetMapping("getArticlePage")
    @ApiOperation(value = "首页新闻/政策")
    public R<Page> getArticlePage(PageDTO pageDTO, ArticleDTO articleDTO) {
        Page<ArticleInfo> page = new Page<>();
        page.setCurrent(pageDTO.getPage());
        page.setSize(pageDTO.getPageSize());
        // 查询图片信息
        return R.ok(catalogService.getArticlePage(page, articleDTO));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.catalogService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param catalog 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Catalog catalog) {
        return R.ok(this.catalogService.save(catalog));
    }

    /**
     * 修改数据
     *
     * @param catalog 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Catalog catalog) {
        return R.ok(this.catalogService.updateById(catalog));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.catalogService.removeByIds(idList));
    }
}

